/*
 *  Copyright (c) 2017, Infineon Technologies AG
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification,are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holders nor the names of its contributors
 *  may be used to endorse or promote products derived from this software without
 *  specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 *  LIABLE  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *  CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *
 *  To improve the quality of the software, users are encouraged to share
 *  modifications, enhancements or bug fixes with Infineon Technologies AG
 *  dave@infineon.com).
 */
/**
 * \defgroup analog_to_digital Analog to digital
 * @{
 */
/**
 * \file    config_adc.h
 * \author  Manuel Escudero Rodriguez
 * \date    06.03.2017
 * \brief   ADC configuration code
 */
#ifndef __CONFIG_ADC_H_
#define __CONFIG_ADC_H_

#include <XMC4200.h>
#include <xmc_vadc.h>

/**
 * \name Channel synchronization definitions
 * @{
 */
#define SYNCHRO_CHANNEL	0
#define MASTER          0
#define SLAVE           1
#define SYNC_REQUIRED   1
/** @ */

/**
 * \name Channel definitions
 * @{
 */
#define IL_CH     	4
#define IO_CH     	6
#define VO_CH      	3
#define UVP_CH     	3
#define VIN_CH     	1
#define NTC_CH     	6
/** Input current measurement channel. */
#define ADC_IL_CH   VADC_G0, IL_CH
/** Input current measurement channel. */
#define ADC_IO_CH   VADC_G0, IO_CH
/** Output voltage measurement channel. */ 
#define ADC_VO_CH   VADC_G0, VO_CH
/** Output under voltage measurement channel. */
#define ADC_UVP_CH  VADC_G1, UVP_CH
/** Input voltage measurement channel. */
#define ADC_VIN_CH  VADC_G1, VIN_CH
/** Temperature measurement channel. */
#define ADC_NTC_CH  VADC_G1, NTC_CH
/** @ */

/**
 * \name Result definitions
 * @{
 */
#define IL_RES    	7
#define IO_RES    	0
#define VO_RES     	15
#define UVP_RES   	0
#define VIN_RES   	7
#define NTC_RES  	15
/** Input current measurement register. */
#define ADC_IL_RES	VADC_G0, IL_RES
/** Input current measurement register. */
#define ADC_IO_RES	VADC_G0, IO_RES
/** Output voltage measurement register. */
#define ADC_VO_RES  VADC_G0, VO_RES
/** Output under voltage measurement register. */
#define ADC_UVP_RES VADC_G1, UVP_RES
/** Input voltage measurement register. */
#define ADC_VIN_RES VADC_G1, VIN_RES
/** Temperature measurement register. */
#define ADC_NTC_RES VADC_G1, NTC_RES
/** @ */

/**
 * \name Channel ADC configuration
 * @{
 */
extern XMC_VADC_CHANNEL_CONFIG_t adc_ch_il_config;
extern XMC_VADC_CHANNEL_CONFIG_t adc_ch_io_config;
extern XMC_VADC_CHANNEL_CONFIG_t adc_ch_vo_config;
extern XMC_VADC_CHANNEL_CONFIG_t adc_ch_uvp_config;
extern XMC_VADC_CHANNEL_CONFIG_t adc_ch_vin_config;
extern XMC_VADC_CHANNEL_CONFIG_t adc_ch_ntc_config;
/** @} */

/**
 * \name ADC measurements
 * @{
 */
/** Current measurement result register. */
#define ADC_IL_RESULT   (VADC_G0->RES[adc_ch_il_config.result_reg_number] & 0xFFFF)
/** Current measurement result register. */
#define ADC_IO_RESULT   (VADC_G0->RES[adc_ch_io_config.result_reg_number] & 0xFFFF)
/** Output voltage measurement result register. */
#define ADC_VO_RESULT   (VADC_G0->RES[adc_ch_vo_config.result_reg_number] & 0xFFFF)
/** Input voltage measurement result register. */
#define ADC_VIN_RESULT  (VADC_G1->RES[adc_ch_vin_config.result_reg_number] & 0xFFFF)
/** Temperature NTC measurement result register. */
#define ADC_NTC_RESULT  (VADC_G1->RES[adc_ch_ntc_config.result_reg_number] & 0xFFFF)
/** @} */

/** Output current measurement is bidirectional and zero value shifted to mid point of rail. [ADC_units] */
#define ADC_IO_MID_POINT_HIGH	(2048 + 10) // Mid of rail plus hysteresis window
/** Output current measurement is bidirectional and zero value shifted to mid point of rail. [ADC_units] */
#define ADC_IO_MID_POINT_LOW	(2048 - 10) // Mid of rail minus hysteresis window
/** Number of trials before triggering fail of output current zero level measurement. [units] */
#define ZERO_ADC_IO_MAX_TRIAL	(10)
/*---------------------------------------------------------------------------*/
void adc_init();
uint16_t die_temp_sens_value();
void die_temp_sens_init();

#endif /* CONFIG_ADC_H_ */
/** @} */
